layui.use(['table', 'jquery', 'layer', 'upload'], function () {
    var table = layui.table,
        layer = layui.layer,
        upload = layui.upload,
        $ = layui.jquery;

    var tableIns = table.render({
        elem: "#projectList",
        height: 'full-125',
        url: ctx + '/sale/projectList',
        page: true,
        cellMinWidth: 80, // 单元格最小宽度
        toolbar: '#toolbarDemo',
        cols: [[ // 表头
            {type: 'checkbox'},
            {type: "numbers"},
            {
                title: "项目编号", field: "projectNo"
            },
            {
                title: "客户名称", field: "basicQuotationBean", templet: function (data) {
                    return data.basicQuotationBean.customerName;
                }
            },
            {
                title: "产品名称", field: "basicQuotationBean", templet: function (data) {
                    return data.basicQuotationBean.productName;
                }
            }, {
                title: "制单人", field: "basicQuotationBean", templet: function (data) {
                    return data.basicQuotationBean.empBean.empName;
                }
            }, {
                title: "数量", field: "basicQuotationBean", templet: function (data) {
                    return data.basicQuotationBean.productCount;
                }
            }, {
                title: "总金额(￥万元)", field: "projectTotalAmount"
            }, {
                title: "下单日期", field: "projectCreateDate"
            }, {
                title: "交付日期", templet: function (data) {
                    return data.basicQuotationBean.deliveryDate;
                }
            }, {
                title: "合同", field: "projectContract"
            }, {
                title: "状态", field: "status", templet: function (data) {
                    if (data.status == "0") {
                        return "待上传"
                    } else if (data.status == "1") {
                        return "生产中"
                    } else if (data.status == "2") {
                        return "质检中"
                    }else if (data.status == "3") {
                        return "已入库"
                    }
                }
            },
            {
                title: "操作 ", sort: true, width: 120, fixed: "right", templet: function (data) {
                    if (data.status == 0) {
                        return '<button class="layui-btn layui-btn-primary layui-btn-xs upload_btn"   value="' + data.id + '">上传</button>' +
                            '<button class="layui-btn layui-btn-warm layui-btn-xs " lay-event="details">详情</button>  ';
                    } else {
                        return '<button class="layui-btn layui-btn-warm layui-btn-xs " lay-event="details">详情</button>  ';
                    }

                }
            }
        ]],
        done: function (data, curr, count) {
            var tableId = this.id;
            var tableElem = this.elem;
            var tableViewElem = tableElem.next();
            var addBtn = tableViewElem.find('.upload_btn')
            upload.render({
                elem: addBtn,
                url: ctx + '/file/contract',
                accept: 'file',
                auto: true,
                multiple: true,
                before: function (obj) {
                    var tableElem = this.item;
                    uploadParm = tableElem.attr('value');
                    layer.load();
                },
                data: {
                    id: function () {
                        return uploadParm;
                    }
                },
                done: function (res, index, upload) {
                    if (res.code == '0' && res.msg == "success") {
                        layer.msg(res.msg, {time: 1000})
                        layer.closeAll('loading')
                        tableIns.reload();
                    }
                },
                error: function (res, index) {
                    layer.msg(res.msg)
                }
            })

        }

    })

    // 监听行工具栏
    table.on('tool(projectFilter)', function (data) {
        if (data.event == "details") {
            toDetailsPage();
        }
    })


    function toDetailsPage() {
        layer.open({})
    }
})

